package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdItemDonate;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdItemDonateDaoImpl extends TempDataObjectDao<StrongholdItemDonate> {
    public static String getStrongholdItemDonateSql = "select * from t_c_activity_stronghold_item_donate where activityId=?";

    public Map<String, Map<Long, Map<Integer, StrongholdItemDonate>>> getStrongholdItemDonate(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Long, Map<Integer, StrongholdItemDonate>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getStrongholdItemDonateSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdItemDonate strongholdItemDonate = parse(rs);
                String unionUid = strongholdItemDonate.getUnionUid();
                long userId = strongholdItemDonate.getUserId();
                if (!dataMap.containsKey(unionUid)) {
                    dataMap.put(unionUid, new ConcurrentHashMap<>());
                }
                Map<Long, Map<Integer, StrongholdItemDonate>> userMap = dataMap.get(unionUid);
                if (!userMap.containsKey(userId)) {
                    userMap.put(userId, new ConcurrentHashMap<>());
                }
                Map<Integer, StrongholdItemDonate> donateMap = userMap.get(userId);
                donateMap.put(strongholdItemDonate.getType(), strongholdItemDonate);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdItemDonateSql = "update t_c_activity_stronghold_item_donate set value=? where activityId=? and unionUid=? and userId=? and type=?";

    @Override
    protected boolean updateData(StrongholdItemDonate strongholdItemDonate) {
        strongholdItemDonate.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdItemDonate.getValue()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdItemDonate.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdItemDonate.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdItemDonate.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdItemDonate.getType()));
        if (getDb().executeNoneQuery(updateStrongholdItemDonateSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdItemDonateSql = "insert into t_c_activity_stronghold_item_donate(activityId,unionUid,userId,type,value) values (?,?,?,?,?)";

    @Override
    protected boolean addData(StrongholdItemDonate strongholdItemDonate) {
        strongholdItemDonate.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdItemDonate.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdItemDonate.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdItemDonate.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdItemDonate.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdItemDonate.getValue()));
        if (getDb().executeNoneQuery(insertStrongholdItemDonateSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdItemDonate parseData(ResultSet rs) throws SQLException {
        StrongholdItemDonate strongholdItemDonate = new StrongholdItemDonate();
        strongholdItemDonate.setActivityId(rs.getInt("activityId"));
        strongholdItemDonate.setUnionUid(rs.getString("unionUid"));
        strongholdItemDonate.setUserId(rs.getLong("userId"));
        strongholdItemDonate.setType(rs.getInt("type"));
        strongholdItemDonate.setValue(rs.getInt("value"));
        return strongholdItemDonate;
    }

}
